package cn.kgc.taolun2.controller;

import cn.kgc.taolun2.pojo.Detail;
import cn.kgc.taolun2.pojo.ResultEntity;
import cn.kgc.taolun2.service.DetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-05-22 12:04
 */
@RestController
public class DetailController {

    @Resource
    DetailService detailService;

    //查询分页
    @RequestMapping("/selectDetail")
    public ResultEntity<PageInfo<Detail>> selectDetail(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "3", required = false) Integer pageSize,
            @RequestParam(value = "sortId", defaultValue = "0", required = false) Integer sortId
    ) {
        PageInfo<Detail> pageInfo = detailService.getPageInfo(pageNum, pageSize, sortId);
        return ResultEntity.successWithoutData(pageInfo);
    }

    //增加
    @RequestMapping("/addDetail")
    public ResultEntity<String> addDetail(@RequestBody Detail detail) {
        try {
            Integer integer = 0;
            if (detail.getId() == null) {
                detailService.addDetail(detail);
            } else {
                detailService.updateDetail(detail);
            }
            return ResultEntity.successWithoutData(integer+"");

        } catch (Exception e) {
          return ResultEntity.failed(e.getMessage());
        }
    }

    //删除
    @RequestMapping("/deleteDetail")
    public ResultEntity<Integer> deleteDetail(Integer id){
        Integer integer = detailService.deleteDetail(id);
        return ResultEntity.successWithoutData(integer);
    }

    //根据id查询
    @RequestMapping("/selectById")
    public ResultEntity<Detail> selectById(Integer id){
        Detail detail = detailService.selectById(id);
        return ResultEntity.successWithoutData(detail);
    }
}
